package org.sylfra.idea.plugins.linessorter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * @author Yangzhuoming
 * @Description: TODO
 * @date 2019/7/17 12:30
 **/
public class GenUtil {
    /**
     * 根据文本生成注释
     * @param origdata
     * @return
     */
    public static String generateZhushi(String origdata) {
        StringBuilder ssPrint = new StringBuilder();
        String line = null;
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(origdata.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));) {
            while ((line = br.readLine()) != null) {
                String reLine = line.replaceAll("//([^\r\n]*)", "/**$1*/");
                String ziduan = reLine.substring(0, reLine.indexOf(';') + 1);
                if (reLine.indexOf("/*") == -1) {
                    ssPrint.append(ziduan + "\n");
                    continue;
                }
                String zhushi = reLine.substring(reLine.indexOf("/*"));
                ssPrint.append(zhushi + "\n");
                ssPrint.append(ziduan + "\n");
            }
        } catch (Exception ex) {
            return origdata;
        }
        return ssPrint.toString();
    }
}